import { createVNode, render, nextTick } from 'vue';
import Confirm from '@/components/common/Confirm.vue';

const container = document.createElement('div');
document.body.appendChild(container);

const $confirm = (options) => {
  return new Promise((resolve, reject) => {
    let visible = true;

    const vnode = createVNode(Confirm, {
      ...options,
      visible, // 初始显示
      onConfirm: () => {
        visible = false;
        render(null, container);
        resolve();
      },
      onCancel: () => {
        visible = false;
        render(null, container);
        reject(new Error('用户取消操作'));
      }
    });
    render(vnode, container);
  });
};

export default {
  install(app) {
    app.config.globalProperties.$confirm = $confirm;
  }
};